/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.isfd.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.comac.ins.common.core.domain.R;
import com.comac.ins.common.core.exception.base.BaseException;
import com.comac.ins.common.idempotent.annotation.RepeatSubmit;
import com.comac.ins.common.log.annotation.Log;
import com.comac.ins.common.log.enums.BusinessType;
import com.comac.ins.common.mybatis.core.page.PageQuery;
import com.comac.ins.common.mybatis.core.page.TableDataInfo;
import com.comac.ins.common.web.core.BaseController;
import com.comac.ins.isfd.domain.bo.IsfdModelGeneralPreprocessBo;
import com.comac.ins.isfd.domain.vo.IsfdModelGeneralPreprocessVo;
import com.comac.ins.isfd.domain.vo.IsfdModelManagementRenderingResultVo;
import com.comac.ins.isfd.service.IIsfdModelGeneralPreprocessService;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * 结构分析通用前处理模型库
 *
 * @author Lion Li
 * @date 2025-08-29
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/isfd/preProcess/model")
public class IsfdModelGeneralPreprocessController extends BaseController {

    private final IIsfdModelGeneralPreprocessService isfdModelGeneralPreprocessService;

    /**
     * 查询结构分析通用前处理模型库列表
     */
    @SaCheckPermission("isfd:modelGeneralPreprocess:list")
    @GetMapping("/list")
    public TableDataInfo<IsfdModelGeneralPreprocessVo> list(IsfdModelGeneralPreprocessBo bo, PageQuery pageQuery) {
        return isfdModelGeneralPreprocessService.queryPageList(bo, pageQuery);
    }

    /**
     * 模型库下载文件
     *
     * @param id 模型库id
     */
    @SaCheckPermission("isfd:modelGeneralPreprocess:download")
    @GetMapping("/download/{id}")
    public void download(@PathVariable Long id, HttpServletResponse response) throws IOException {
        isfdModelGeneralPreprocessService.download(id, response);
    }

    /**
     * 文件上传结构分析通用前处理模型库
     */
    @SaCheckPermission("isfd:modelGeneralPreprocess:add")
    @Log(title = "结构分析通用前处理模型库", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/save")
    public R<Void> save(@RequestParam("file") MultipartFile file,
                          @NotEmpty(message = "机型编号不能为空") @RequestParam("modelNumber") String modelNumber,
                          @NotEmpty(message = "批次编号不能为空") @RequestParam("batchNumber") String batchNumber,
                        @NotEmpty(message = "部段编号不能为空")@RequestParam("sectionNumber") String sectionNumber,
                        @RequestParam(required = false, value = "remarks") String remarks) {
        if (file == null || file.isEmpty()) {
            throw new BaseException("上传文件不能为空");
        }
        IsfdModelGeneralPreprocessBo bo = new IsfdModelGeneralPreprocessBo();
        bo.setModelNumber(modelNumber);
        bo.setBatchNumber(batchNumber);
        bo.setSectionNumber(sectionNumber);
        bo.setRemarks(remarks);
        return toAjax(isfdModelGeneralPreprocessService.save(file, bo));
    }

    /**
     * 删除结构分析通用前处理模型库
     *
     * @param ids 主键串
     */
    @SaCheckPermission("isfd:modelGeneralPreprocess:remove")
    @Log(title = "结构分析通用前处理模型库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(isfdModelGeneralPreprocessService.deleteWithValidByIds(List.of(ids), true));
    }


    /**
     * 模型渲染
     */
    @Log(title = "模型渲染", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @GetMapping("/load")
    public R<IsfdModelManagementRenderingResultVo> loadModel(@RequestParam("id") Long id) {
        return R.ok(isfdModelGeneralPreprocessService.loadModel(id));
    }

}
